/*
 * OEnum
 * 
 * Creates a new enum
 * 
 * Usage:
 * 	var enum = OEnum("one", "two", "three");
 * 
 */

objx("objx.enum").requires("objx.class");

var OEnumBase = OClass({
	init: function(name, value){
		this._name = name;
		this._value = value;
	},
	getValue: function(){
		return this._value;
	},
	getName: function(){
		return this._name;
	}
});

OEnumBase.fromValue = function(value){
	
	var foundItem = null;
	
	objx(this.names).each(function(name){
		
		var item = this.instances[name];
		
		if (item.getValue() == value) {
			foundItem = this.instances[name];
			return false;
		}
		
	}.bind(this));
	
	return foundItem;
	
};

var OEnum = function(){
	
	var enumClass = OClass(OEnumBase, {});
	
	enumClass.names = [];
	enumClass.instances = {};
	enumClass.fromValue = OEnumBase.fromValue;
	
	if (arguments.length === 1 && typeof arguments[0] === "object") {
		
		objx(arguments[0]).each(function(value, item){
			
			enumClass[item] = new enumClass(item, value);
			enumClass.instances[item] = enumClass[item];
			enumClass.names.push(item);
			
		});
		
	} else {
		
		objx(arguments).each(function(item, index){
			
			enumClass[item] = new enumClass(item, index);
			enumClass.instances[item] = enumClass[item];
			enumClass.names.push(item);
			
		});
		
	}
	
	return enumClass;
	
};

OProvides("objx.enum");